//-----------------------------------------------------------------------------
// Name: Billboard.h
// Auth: Bradford J. Smith
// Desc: a simple textured quad class
//-----------------------------------------------------------------------------

#include "Billboard.h"

#include "Assert.h"

#include "corona/corona.h"

#include <GL/glut.h>

//-----------------------------------------------------------------------------
// Singleton Pattern:
//-----------------------------------------------------------------------------
cBillboard gBillboard;

cBillboard* GetBillboard()
{
	return &gBillboard;
}

//-----------------------------------------------------------------------------
// Billboard:
//-----------------------------------------------------------------------------
cBillboard::cBillboard() :
	mSize(0.0f),
	mImageData(0)
{

}

cBillboard::~cBillboard()
{

}

void cBillboard::SetSize(float size)
{
	// make sure we have a non-negative size.
	Assert(size >= 0.0f);
	mSize = size;
}

/*
float cBillboard::GetSize()
{
	return mSize;
}
*/

////void cBillboard::SetPosition(const tVector3f &position)
////{
////	mPosition[0] = position[0];
////	mPosition[1] = position[1];
////	mPosition[2] = position[2];
////}

void cBillboard::SetImage(corona::Image* pImage)
{
	mImageData = pImage;
}

void cBillboard::Draw()
{
	// only draw a quad if it has a size greater than 0.
	if (mSize > 0)
	{
		// store old state
		GLboolean isLightingEnabled = glIsEnabled(GL_LIGHTING);

		// disable lighting
		glDisable(GL_LIGHTING);

		glColor3f(1.0f, 1.0f, 1.0f);

		glMatrixMode(GL_MODELVIEW);
		//glPushMatrix();

		// set the texture data if any is present
		if (mImageData != 0)
		{
			// enable texturing
			glEnable(GL_TEXTURE_2D);
			glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

			glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
			glTexImage2D(GL_TEXTURE_2D, 0, 4, 
						 mImageData->getWidth(), mImageData->getHeight(), 
						 0, GL_RGBA, GL_UNSIGNED_BYTE, mImageData->getPixels());
		}
		else
		{
			// if there's no texture, we need to disable texturing
			glDisable(GL_TEXTURE_2D);
		}

		// move to position
		//glTranslatef(mPosition[0], mPosition[1], mPosition[2]);
		//glRotatef(rotation * kRadiansToDegrees, 0.0f, 0.0f, -1.0f);

		float radius = mSize / 0.1f;


		// draw a quad facing down the z axis.
		glPushMatrix();
		//glTranslatef(0.0f, 0.0f, kStart);
		glBegin(GL_QUADS);
		glNormal3f(0.0f, 0.0f, 1.0f);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-kSides, -kSides, kStart);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(kSides, -kSides, kStart);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(kSides, kSides, kStart);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-kSides, kSides, kStart);
		glEnd();
		glPopMatrix();

		glPushMatrix();
		glBegin(GL_QUADS);
		glNormal3f(-1.0f, 0.0f, 0.0f);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(kSides, -kSides, kStart);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(kSides, -kSides, -kStart);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(kSides, kSides, -kStart);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(kSides, kSides, kStart);
		glEnd();
		glPopMatrix();

		glPushMatrix();
		glBegin(GL_QUADS);
		glNormal3f(1.0f, 0.0f, 0.0f);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-kSides, -kSides, -kStart);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(-kSides, -kSides, kStart);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(-kSides, kSides, kStart);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-kSides, kSides, -kStart);
		glEnd();
		glPopMatrix();

		glPushMatrix();
		glBegin(GL_QUADS);
		glNormal3f(0.0f, -1.0f, 0.0f);
		glTexCoord2f(0.0f, 0.0f);
		glVertex3f(-kSides, kSides, kStart);
		glTexCoord2f(1.0f, 0.0f);
		glVertex3f(kSides, kSides, kStart);
		glTexCoord2f(1.0f, 1.0f);
		glVertex3f(kSides, kSides, -kStart);
		glTexCoord2f(0.0f, 1.0f);
		glVertex3f(-kSides, kSides, -kStart);
		glEnd();
		glPopMatrix();



		// glDisable(GL_BLEND);
		// restore the lighting settings
		if (isLightingEnabled)
		{
			glEnable(GL_LIGHTING);
		}
	}
}
